package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.ApiUserService;
import com.platform.service.ApiWithdrawService;
import com.platform.service.SysConfigService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "提现管理")
@RestController
@RequestMapping("/api/withdraw")
public class ApiWithdrawController extends ApiBaseAction {
    @Autowired
    private ApiWithdrawService apiWithdrawService;

    @Autowired
    private ApiUserService apiUserService;

    @ApiOperation(value = "申请提现")
    @PostMapping("/apply")
    public Object apply(@LoginUser UserVo loginUser, WithdrawVo withdrawVo) {
        if (withdrawVo.getApplyMoney() == null){
            return toResponsFail("申请提现金额为空");
        }

        if (withdrawVo.getTransferType() == null
                || !withdrawVo.getTransferType().equals(WithdrawVo.TRANSFER_TYPE_CARD)
                || withdrawVo.getTransferType().equals(WithdrawVo.TRANSFER_TYPE_WX)){
            return toResponsFail("提现方式不正确");
        }

        //判断提现金额
        UserVo userVo = apiUserService.queryObject(loginUser.getUserId());
        if (userVo.getAccount().compareTo(BigDecimal.ONE) < 0){
            return toResponsFail("余额1元后才能申请提现");
        }

        //是否存在已申请的
        WithdrawVo applying = apiWithdrawService.getApplying(loginUser);
        if (applying != null){
            return toResponsFail("存在待打款的提现申请，无法再次提交");
        }

        withdrawVo = apiWithdrawService.apply(loginUser, withdrawVo);
        return toResponsSuccess("申请成功，申请订单号为：" + withdrawVo.getWithdrawSn());
    }


    @ApiOperation(value = "申请提现列表")
    @PostMapping("/list")
    public Object list(@LoginUser UserVo loginUser,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map param = new HashMap();
        param.put("page", page);
        param.put("limit", size);
        param.put("applyUserId", loginUser.getUserId());
        param.put("sidx", "apply_date");
        param.put("order", "desc");
        param.put("fields", "id, apply_date, apply_money, status");
        //查询列表数据
        Query query = new Query(param);
        List<WithdrawVo> topicEntities = apiWithdrawService.queryList(query);
        int total = apiWithdrawService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(topicEntities, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }
}